const { ListNode, LinkedList } = require('../2. 链表/1. 链表基础/1. 建立线性链表.js')
// 给定一个单链表L的头节点head， 0,1,2,3,4.....n-1,n
// 要求将单链表重新排列，0,n,1,n-1,2,n-2.....
// 思路，将链表转为数组操作

function reorderList(head) {
    if (!head) {
        return null
    }
    let vec = []
    let cur = head
    while (cur) {
        vec.push(cur)
        cur = cur.next
    }
    let left = 0
    let right = vec.length - 1
    while (left < right) {
        vec[left].next = vec[right]
        left++
        if (left === right) {
            break
        }
        vec[right].next = vec[left]
        right--
    }
    vec[left].next = null
    return head
}

let node = new LinkedList([1,2,3,4,5,6,7])
reorderList(node.head)
console.log(node.head)